//
//  UserManager.m
//  SPDemo
//
//  Created by Sailer Guo on 2020/1/6.
//  Copyright © 2020 Sailer Guo. All rights reserved.
//

#import "LoginManager.h"
#import "Environment.h"

@interface LoginManager ()

@property (nonatomic,strong,readwrite) SPUserInfoModel *userInfo;

@end

@implementation LoginManager

- (RACSignal *)userLogin:(NSString *)account pwd:(NSString *)pwd {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    return [[[Environment sharedInstance].apiService loginAccount:account pwd:pwd] flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            // 在这里更新用户信息，特别是用户标识信息
            [userDefault setObject:@"xxx_userId" forKey:kMemberSid];
            [userDefault synchronize];
            
            self.userInfo = [[SPUserInfoModel alloc] init];
            
            return [RACDisposable disposableWithBlock:^{
                
            }];
        }];
    }];
}

- (RACSignal *)loginOut {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

    return [[[Environment sharedInstance].apiService logout] flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 用户登出，看业务是否支持游客模式了
                [userDefault setObject:@"xxx_advistor_id" forKey:kMemberSid];
                [userDefault synchronize];
                
                return [RACDisposable disposableWithBlock:^{
                    
                }];
            }];
        }];
        
}

@end
